from django.db import models


class City(models.Model):
    name = models.CharField(max_length=100)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name


class DataCenter(models.Model):
    name = models.CharField(max_length=100)
    city = models.ForeignKey(City, on_delete=models.CASCADE, related_name='data_centers')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return f"{self.city} - {self.name}"


class Host(models.Model):
    ip_address = models.GenericIPAddressField(unique=True)
    hostname = models.CharField(max_length=255, blank=True, null=True)
    data_center = models.ForeignKey(DataCenter, on_delete=models.SET_NULL, null=True, related_name='hosts')
    root_password = models.CharField(max_length=255)  # 存储加密后的密码
    is_active = models.BooleanField(default=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.ip_address


class HostStatistics(models.Model):
    date = models.DateField(auto_now_add=True)
    total_hosts = models.IntegerField()
    stats = models.JSONField()  # 使用JSONField存储统计详情
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"{self.date} - {self.total_hosts} hosts"
